package edu.lnu.project.timeeditapp.activites;

import com.lnu.project.timeeditapp.R;

import edu.lnu.project.timeeditapp.helper.MyHelper;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class StartActivity extends Activity {
	private Activity mainActivity;

	public static SharedPreferences initializePref;
	public static SharedPreferences.Editor initializePrefEditor;
	public static final String INITIALIZED = "initialized";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_start);
		initializePref = PreferenceManager.getDefaultSharedPreferences(this);
		initializePrefEditor = initializePref.edit();
		
		mainActivity = this;

		//Create buttons and add listeners
		setUpButton();
		
		//Set up the action bar
		getActionBar();

	}

	public void startService(){
		Intent intent = new Intent(mainActivity.getApplicationContext(), ReminderService.class );
		mainActivity.startService(intent);
	}

	/**
	 * Create buttons and add listeners
	 */
	private void setUpButton(){

		final Button iniButton = (Button)findViewById(R.id.iniButton);
		final Button startButton = (Button)findViewById(R.id.startButton);


		iniButton.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				/* Start new Activity SelectFileActivity */
				startService();
				Intent intent = new Intent(mainActivity, SelectFileActivity.class);
				mainActivity.startActivity(intent);


			}});

		startButton.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				if(initializePref.getBoolean(INITIALIZED, false)){
					Intent intent = new Intent(mainActivity, ListViewActivity.class);
					//initializeSharedPreferences(mainActivity);
					mainActivity.startActivity(intent);
				}
				else{
					Toast.makeText(mainActivity, "Please touch on the Initialize Button!", Toast.LENGTH_SHORT).show();
					iniButton.requestFocus();
				}

			} 
		});
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.action_menu_startactivity, menu);

		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) { 

		switch (item.getItemId()) {
		case R.id.actionMenuStartActivityFinish:  	
			finish();
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}

	@Override
	protected void onDestroy() {
		Intent intent = new Intent(mainActivity.getApplicationContext(), ReminderService.class );
		mainActivity.stopService(intent);
		super.onDestroy();
	}
}
